package com.strawcom.charmring.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.animation.DecelerateInterpolator;
import android.widget.FrameLayout;
import android.widget.Scroller;

import com.strawcom.charmring.R;

/**
 * Exercise time information records
 * 
 * @author WangPeng
 */
public class RiseSlidingLayout extends FrameLayout {
	private int mHeight;

	private Scroller mScroller;
	private boolean isAnim = false;
	private boolean is2OpenContent = false;
	private boolean isContentOpened = false;
	private int mDuration = 400;
	private int contentTop;
	private int touchSlop;
	private float downX, downY;

	private View layHeadSwitch;

	public RiseSlidingLayout(Context context) {
		this(context, null);
	}

	public RiseSlidingLayout(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RiseSlidingLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		mScroller = new Scroller(context, new DecelerateInterpolator());
		touchSlop = ViewConfiguration.get(context).getScaledTouchSlop();

		LayoutInflater.from(context).inflate(R.layout.lay_exercise_daily_record_list, this);
		layHeadSwitch = findViewById(R.id.lay_head_switch);

//		returnBottom();
	}
	
	public void returnBottom() {
		layHeadSwitch.post(new Runnable() {
			@Override public void run() {
				is2OpenContent = false;
				isContentOpened = false;
				contentTop = mHeight - layHeadSwitch.getHeight();
				scrollTo(0, -contentTop);
			}
		});
	}

	@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		mHeight = h;
		
		returnBottom();
	}

	@Override public boolean onTouchEvent(MotionEvent event) {
		if (!isAnim) {
			switch (event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				downX = event.getX();
				downY = event.getY();
				if (!isContentOpened && downY < contentTop) {
					break;
				}
				if (listener != null) {
					listener.onSlidingStart();
				}
				return true;
			case MotionEvent.ACTION_MOVE:
				float ty = (int) (downY - event.getY() - (isContentOpened ? 0 : contentTop));
				if (isContentOpened && ty > 0) {
					ty = 0;
				}
				else if (!isContentOpened && -ty > contentTop) {
					ty = -contentTop;
				}
				scrollTo(0, (int) ty);
				break;
			case MotionEvent.ACTION_CANCEL:
			case MotionEvent.ACTION_UP:
				if (Math.abs(downX - event.getX()) < touchSlop && Math.abs(downY - event.getY()) < touchSlop) {
					// click
					if (isContentOpened && downY < mHeight - contentTop) {
						is2OpenContent = false;
					}
					else if (!isContentOpened && downY > contentTop) {
						is2OpenContent = true;
					}
					else {
						return super.onTouchEvent(event);
					}
				}
				else {
					is2OpenContent = (isContentOpened && -getScrollY() < mHeight * 0.2f)
							|| (!isContentOpened && -getScrollY() < mHeight * 0.8f);
				}
				dealTouchMoveResult();

				break;
			}
		}
		return super.onTouchEvent(event);
	}

	private void dealTouchMoveResult() {
		if (is2OpenContent) {
			startMoveAnim(getScrollY(), -getScrollY(), mDuration);
			isContentOpened = true;
		}
		else {
			startMoveAnim(getScrollY(), -getScrollY() - contentTop, mDuration);
			isContentOpened = false;
		}
		postDelayed(new Runnable() {
			@Override public void run() {
				if (listener != null) {
					listener.onSlidingOver(isContentOpened);
				}
			}
		}, mDuration);
	}

	private void startMoveAnim(int startY, int dy, int duration) {
		isAnim = true;
		mScroller.startScroll(0, startY, 0, dy, duration);
		invalidate();
	}

	@Override public void computeScroll() {
		if (mScroller.computeScrollOffset()) {
			scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
			postInvalidate();
			isAnim = true;
		}
		else {
			isAnim = false;
		}
		super.computeScroll();
	}

	private OnSlidingStateChangedListener listener;

	public void setOnSlidingStateChangedListener(OnSlidingStateChangedListener listener) {
		this.listener = listener;
	}

	public interface OnSlidingStateChangedListener {
		void onSlidingStart();

		void onSlidingOver(boolean isOpen);
	}
}